<?php
/**
 * Created by PhpStorm.
 * User: zy-l 
 * Date: 2019/6/19
 * Time: 1:23 AM
 */

namespace App\HttpController\Admin;

use App\Common\Exception\ThrowException;
use App\HttpController\AdminBase;
use App\Model\ConfigClass;
use EasySwoole\Validate\Validate;

class Config extends AdminBase
{

    /**
     * 配置模型
     * @return \App\Model\Config
     */
    public function getConfigModel()
    {
        return new \App\Model\Config();
    }

    /**
     *
     * @return ConfigClass
     */
    public function getConfigClassModel()
    {
        return new ConfigClass();
    }

    /**
     * 分配设置
     * @return string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function index()
    {
        $menuList = $this->getConfigClassModel()->getConfigClassMenuListAll(['status' => 1,'list_order|order'=>'desc']);
        $menuArr = array();
        foreach ($menuList as $k => $item) {
            $menuArr[$k]["id"] = $item['id'];
            $menuArr[$k]["name"] = $item['name'];
            $menuArr[$k]["pId"] = $item['pid'];
        }
        $this->assign(['menu_list' => json_encode($menuArr)]);
        return $this->fetch();
    }

    /**
     * 配置管理
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function group()
    {
        if ($this->isPost()) {
            $data=$this->request()->getRequestParam();
            $where = array();
            $title = $this->postParam("title");
            if (!empty($title)) {
                $where["title|like"] = '%' . $title . '%';
            }
            $name = $this->postParam("name");
            if (!empty($name)) {
                $where["name|like"] = '%' . $name . '%';
            }
            $value = $this->postParam("value");
            if (!empty($value)) {
                $where["value|like"] = '%' . $value . '%';
            }
            $type = $this->postParam("type");
            if (!empty($type)) {
                $where["type"] = $type;
            }
           return  $this->writePageJson('操作成功', $this->getConfigModel()->getConfigList($data,$where)) ;
        }
        $menuList = $this->getConfigClassModel()->getConfigClassMenuListAll(['status' => 1,'list_order|order'=>'desc']);
        $menuArr = array();
        foreach ($menuList as $k => $item) {
            $menuArr[$k]["id"] = $item['id'];
            $menuArr[$k]["name"] = $item['name'];
            $menuArr[$k]["pId"] = $item['pid'];
        }
        $this->assign(['menu_list' => json_encode($menuArr)]);
        return $this->fetch();
    }

    public function classedit(){
        $id=$this->postParam('id');
        return $this->writeJson('操作成功',$this->getConfigClassModel()->getInfo(['id'=>$id]));
    }

    /**
     * 配置添加
     * @return string|void
     */
    public function add(){
        if($this->isPost()){
            return $this->writeJson('操作成功',$this->getConfigModel()->updateOrCreateData($this->postParams()));
        }
        $menuid = $this->getParam('menuid');
        $this->assign(["menuid"=>$menuid]);
        return $this->fetch();
    }

    /**
     * 配置编辑
     * @return bool|string|void
     * @throws ThrowException
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function edit(){
        if ($this->isPost()) {
            return $this->writeJson('操作成功',$this->getConfigModel()->updateOrCreateData($this->postParams(),["id"=>$this->postParam("id")]));
        }
        $id=$this->getParam('id');
        if (empty(intval($id))) {
            throw new ThrowException([422,'请选择要修改数据']);
        } else {
            $info=$this->getConfigModel()->findOne(["id"=>$id]);
          echo $this->getConfigModel()->getLastQuery();
            $this->assign(["info"=>$info]);
            //加载模板
            return $this->fetch();
        }
    }

    /**
     * 配置项删除
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function del(){
        return $this->writeJson('操作成功', $this->getConfigModel()->delData(["id" => $this->postParam("id")]));
    }

    /**
     *
     */
    public function classsave(){
        if($this->isPost()){
            return $this->writeJson('操作成功',$this->getConfigClassModel()->updateOrCreateData($this->postParams(),["id"=>$this->postParam("id")]));
        }
    }

    /**
     * 分类删除
     * @return bool
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function classdel(){

        return $this->writeJson($this->getConfigClassModel()->delData(["id"=>$this->postParam("id")]));
    }



    /**
     * 提交参数验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;
        if ($this->isPost()) {
            switch ($action) {
                case 'classsave';  //操作方法
                    $validate = new Validate();
                    $validate->addColumn('pname', '分类名称')->required('不能为空');
                    break;
            }
        }
        return $validate;
    }
}